<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\wechat\model;

class Menu extends \suifeng\Model
{
    protected $name = 'wechat_mp_menu';

    public function get_menu_tree($mp_id, $root_id = 0, $exclude_ids = null)
    {
        $map['status'] = array('eq', 1);
        $map['mp_id']  = array('eq', $mp_id);
        $list = $this->where($map)->order('sort asc, id asc')->select();

        $tree = new \suifeng\Tree();
        return $tree->list2tree($list, 'id', 'pid', '_child', $root_id, $exclude_ids);
    }

    public function get_array_tree($mp_id, $root_id, $exclude_ids = null)
    {
        $map['status'] = array('eq', 1);
        $map['mp_id']  = array('eq', $mp_id);
        $list = $this->where($map)->order('sort asc, id asc')->select();

        $tree = new \suifeng\Tree();
        $list = $tree->array2tree($list, 'title', 'id', 'pid', '_child', $root_id, $exclude_ids);
        return $list;
    }

    public function get_list_tree($mp_id, $root_id, $exclude_ids = null)
    {
        $map['status'] = array('eq', 1);
        $map['mp_id']  = array('eq', $mp_id);
        $list = $this->where($map)->order('sort asc, id asc')->select();

        $tree = new \suifeng\Tree();
        $list = $tree->list2tree($list, 'id', 'pid', '_child', $root_id, $exclude_ids);

        $result = array();
        $list = $tree->tree2list($list, $result);
        return $result;
    }

    public function delete_recursion($id)
    {
        $child_ids = $this->where('pid', $id)->column('id');
        foreach ($child_ids as $child_id) {
            $this->delete_recursion($child_id);
        }
        $this->where('id', $id)->delete();
    }
}
